package com.sd.jd.collection_test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * date: 2024/3/14
 */
public class CollTest {

    public static void main(String[] args) {
        // 初始化集合
        {
            List<String> list = List.of("apple", "banana", "orange");
            Set<Integer> set = Set.of(1, 2, 3, 4);
            Map<String, Integer> map = Map.of("apple", 1, "banana", 2, "orange", 3);
        }


        // 8也支持
        {
            List<String> list = List.of("apple", "banana", "orange");
            String joinedString = list.stream().collect(Collectors.joining("-", "[", "]"));
            System.out.println(joinedString);

            Map<String, Integer> map = Map.of("apple", 1, "banana", 2, "orange", 3);
            Map<Integer, String> reversedMap = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
            System.out.println(reversedMap);
        }

        // takeWhile() 方法和 dropWhile() 方法：根据条件截取集合
        {
            List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
            List<Integer> takenList = list.stream().takeWhile(number -> number < 5).collect(Collectors.toList());
            System.out.println(takenList);

            List<Integer> dropedList = list.stream().dropWhile(number -> number < 5).collect(Collectors.toList());
            System.out.println(dropedList);
        }

        // toArray(IntFunction<T[]>) 方法：返回集合中的所有元素到一个新数组中
        {
            List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
            //Object[] array = list.toArray(); 这个是8的用法
            Integer[] array = list.toArray(Integer[]::new);
            System.out.println(Arrays.toString(array));
        }
    }
}
